package com.x.sys.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.x.common.bo.PageObject;
import com.x.common.exception.ServiceException;
import com.x.sys.dao.SysItemDao;
import com.x.sys.entity.SysItem;
import com.x.sys.service.SysItemService;

@Service
	public class SysItemServiceImpl implements SysItemService {
	@Autowired
		private SysItemDao sysItemDao;

		@Override
		public PageObject<SysItem> findPageObjects(String name, Integer pageCurrent) {
			//参数校验
			
			if(pageCurrent==null||pageCurrent<1)
			throw new IllegalArgumentException("页码值无效");
			//查询总记录数,并进行校验
			int rowCount=sysItemDao.getRowCount(name);
			if(rowCount==0)
			throw new ServiceException("没有对应的记录");
			//查询当前页记录
			int pageSize=4;
			//计算当前页查询的起始位置
			int startIndex=(pageCurrent-1)*pageSize;
			List<SysItem> records=
					sysItemDao.findPageObjects(name, startIndex, pageSize);
			//对业务层查询结果进行处理和封装
			return new PageObject<>(records, rowCount, pageSize, pageCurrent);
		}

		@Override
		public int deleteObjects(Integer... ids) {
			//1.参数校验
			if(ids==null||ids.length==0)
				throw new IllegalArgumentException("参数值无效");
			//2.执行删除操作
			int rows=sysItemDao.deleteObjects(ids);
			//3.验证删除结果
			if(rows==0)
				throw new ServiceException("记录可能不存在了");
			return rows;
		}


		@Override
		public void saveObject(SysItem entity) {
			//1.参数有效性校验
			if(entity==null)
				throw new IllegalArgumentException("保存对象不能为空");
			if(StringUtils.isEmpty(entity))
				throw new IllegalArgumentException("角色名不允许为空");
			//将用户对应的角色信息写入导信息库
			sysItemDao.insertObject(entity);
			
		}

		
		
		
	}


